package com.ym.pms.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ym.pms.dao.CommentsMapper;
import com.ym.pms.dao.ContractMapper;
import com.ym.pms.dao.ListoffeesMapper;
import com.ym.pms.model.Comments;
import com.ym.pms.model.Contract;
import com.ym.pms.model.Listoffees;
import com.ym.pms.service.CommentsServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.logging.Level;

/**
 * @Author ming
 * @Description
 * @Date 2020/12/16/016 21:24
 **/
@Service
public class CommentsServiceImpl implements CommentsServices {
    @Autowired
    CommentsMapper commentsMapper;
    @Autowired
    ListoffeesMapper listoffeesMapper;
    @Autowired
    ContractMapper contractMapper;
    @Override
    public List<Comments> findByReceiverId(Integer id) {
        return commentsMapper.findByReceiverId(id);
    }

    @Override
    public int add(Comments comments) {
        return commentsMapper.add(comments);
    }

    @Override
    public int del(Integer id) {
        return commentsMapper.del(id);
    }

    @Override
    public int read(Integer id) {
        return commentsMapper.read(id);
    }

    @Override
    public Comments findById(Integer id) {
        return commentsMapper.findById(id);
    }

    @Override
    public List<Comments> findBySenderId(Integer id) {
        return commentsMapper.findBySenderId(id);
    }

    @Override
    //    每月10号0点开始提醒未缴费用户缴费
    @Scheduled(cron = "0 0 0 10 * *")
    public void AlertPay() {
        QueryWrapper<Listoffees> wrapper = new QueryWrapper<>();
        wrapper.eq("lof_payStatus",0);
        List<Listoffees> listoffees = listoffeesMapper.selectList(wrapper);
        for (Listoffees listoffee : listoffees) {
            Comments comments = new Comments();
            comments.setUserId(listoffee.getLofUid());
//            id0号为系统自动发送
            comments.setAdminId(0);
            comments.setTitle("请尽快缴费");
            comments.setContent("您本月房租水电还未交，15号后未交，合同将自动终止！！！");
            comments.setRank(2);
            comments.setCreateTime(new Date());
            commentsMapper.add(comments);
        }
    }

    @Override
    //    每月16号0点扫描数据库，终止逾期期未交钱人的合同
    @Scheduled(cron = "0 0 0 16 * *")
    public void gun() {
        QueryWrapper<Listoffees> wrapper = new QueryWrapper<>();
        wrapper.eq("lof_payStatus",0);
        wrapper.lt("lof_DTime",new Date());
        List<Listoffees> listoffees = listoffeesMapper.selectList(wrapper);
        for (Listoffees listoffee : listoffees) {
//            通知合同终止
            Comments comments = new Comments();
            comments.setUserId(listoffee.getLofUid());
//            id0号为系统自动发送
            comments.setAdminId(0);
            comments.setTitle("合同终止通知");
            comments.setContent("您本月房租水电还未交，已逾期，租房合同终止！");
            comments.setRank(2);
            comments.setCreateTime(new Date());
            commentsMapper.add(comments);

//            终止合同
            contractMapper.updateInfo(listoffee.getLofCoid(),"逾期未交租，合同自动终止！",new Date());

        }
    }
}
